package com.zzf.net.testDomain;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.net.URL;

import com.google.gson.Gson;
import com.zzf.Base64;
import com.zzf.net.HttpClient;
import com.zzf.net.testDomain.req.BaseReqArg;
import com.zzf.net.testDomain.req.CheckDomainReq;
import com.zzf.net.testDomain.req.CreateContactReq;
import com.zzf.net.testDomain.req.CreateDnsReq;
import com.zzf.net.testDomain.req.DeleteContactReq;
import com.zzf.net.testDomain.req.ModifyContactDetailReq;
import com.zzf.net.testDomain.req.ModifyContactReq;
import com.zzf.net.testDomain.req.ModifyDnsReq;
import com.zzf.net.testDomain.req.ModifyDnsStatusReq;
import com.zzf.net.testDomain.req.ModifyNsReq;
import com.zzf.net.testDomain.req.RegisterReq;
import com.zzf.net.testDomain.req.RenewReq;
import com.zzf.net.testDomain.req.UploadContactImageReq;

public class Test {
	
	//正式环境
	public static final String URLPATH = "http://dl.yuminghome.com/";
	public static final String USERID="3007";
	public static final String APIKEY="3dcc966b6b4a7ffc824958e09c9b2927";

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
//		testCreateContact();
		uploadContactImage();
	}
	
	public static void com(BaseReqArg req,String url)throws Exception{
		Gson gs = new Gson();
		String jsonstr = gs.toJson(req);
		System.out.println("**********params="+jsonstr);
		byte[] b =	HttpClient.PostData(new URL(url), jsonstr.getBytes("utf-8"));
		System.out.println(new String(b));
	}
	
	/**
	 * 检测能否注册
	 * @throws Exception
	 */
	public static void testCheckDomain()throws Exception{
		String url = Test.URLPATH + "API/ApiDomain/checkDomain.json";
		CheckDomainReq req = new CheckDomainReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.domain="ssss.com";
		com(req,url);
	}
	
	/**
	 * 注册
	 * @throws Exception
	 */
	public static void testRegister()throws Exception{
		String url = Test.URLPATH + "API/ApiDomain/register.json";
		RegisterReq req = new RegisterReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.domain="testsssss.com";
		req.years=1;
		req.contactId=31182281L;
		com(req,url);
	}
	
	/**
	 * 续费
	 * @throws Exception
	 */
	public static void testRenew()throws Exception{
		String url = Test.URLPATH + "API/ApiDomain/renew.json";
		RenewReq req = new RenewReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.domain="gongxifacaiapp1234.net";
		req.years=1;
		com(req,url);
	}
	
	/**
	 * 修改DNS
	 * @throws Exception
	 */
	public static void testModifyNs()throws Exception{
		String url = Test.URLPATH + "API/ApiDomain/modifyNs.json";
		ModifyNsReq req = new ModifyNsReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.domain="ssss.com";
		req.newAddress = new String[]{"ns1.yuming.com","ns2.yuming.com"};
		com(req,url);
	}
	
	/**
	 * 域名过户（修改域名的联系人信息）
	 * @throws Exception
	 */
	public static void testModifyContact()throws Exception{
		String url = Test.URLPATH + "API/ApiDomain/modifyContact.json";
		ModifyContactReq req = new ModifyContactReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.domain="ssss.com";
		req.contactId = 1L;
		com(req,url);
	}
	
	/**
	 * 获得缺省的DNS服务
	 * @throws Exception
	 */
	public static void testGetDefaultNs()throws Exception{
		String url = Test.URLPATH + "API/ApiDomain/getDefaultNs.json";
		BaseReqArg req = new BaseReqArg();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		com(req,url);
	}
	
	/**
	 * 创建一个新的联系人
	 * @throws Exception
	 */
	public static void testCreateContact()throws Exception{
		String url = Test.URLPATH + "API/ApiContact/createContact.json";
		CreateContactReq req = new CreateContactReq();
//		String image=Base64.ToBase64String(getImage("d:/DomainCertificate.jpg"));
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.email="25462123@qq.com";
		req.postcode="510000";
		req.tel="+86.18612345678";
		req.fax="+86.2066222604";
		req.province="广东";
		req.province_en="GuangDong";
		req.city="广州";
		req.city_en="GuangZhou";
		req.area="天河区";
		req.addr="林和西路1号国际贸易中心31楼";
		req.cnt_id="440104198302245791";//身份证号码
//		req.cnt_image = image;
		req.isperson="N";
		
//		req.type = "INTERNAL";
//		req.country="中国";
//		req.country_en="CN";
//		req.contacter="张三";//当type=INTERNAL时表示联系人(中文)。
//		if("N".equals(req.isperson)){
//			req.owner="翼优";//当type=INTERNAL时，如果isperson=N则表示企业名称（中文），isperson=Y则不是必须的。
//			req.enterprise_id="123456";//isperson为N时才有
//			req.enterprise_image = image;//isperson为N时才有
//		}

		req.type = "INTERNATIONAL";//（当type=EU时，必须为欧盟国家）
		req.country="法国";
		req.country_en="FR";
		req.owner="张三";// 当type=INTERNATIONAL时，如果isperson=N则表示企业名称(中文)，isperson=Y表示域名所有者中文名称
		req.owner_en="zhang san";//如果isperson=N则表示企业名称(英文)，isperson=Y表示域名所有者英文名称 当type=INTERNATIONAL时才必须填写。
		req.contacter_first_name="张";//联系人(中文)，姓。 当type=INTERNATIONAL时才必须填写。
		req.contacter="三";//当type=INTERNATIONAL时表示联系人(中文)，名 ； 
		req.contacter_first_name_en="zhang";//联系人(英文)，姓。 当type=INTERNATIONAL时才必须填写。
		req.contacter_en="san";//联系人(英文)，名。 当type=INTERNATIONAL时才必须填写。
		req.addr_en="Lin He Xi Lu 1Hao Guo Ji Mao Yi Zhong Xin 31Lou ";//联系地址(英文)。 当type=INTERNATIONAL时才必须填写。
		if("N".equals(req.isperson)){
			req.enterprise_id="粤A12345";//isperson为N时才有
//			req.enterprise_image = image;//isperson为N时才有
		}
		
		com(req,url);
	}

	
	/**
	 * 上传联系人证件复印件
	 * @throws Exception
	 */
	public static void uploadContactImage()throws Exception{
		String url = Test.URLPATH + "API/ApiContact/uploadContactImage.json";
		UploadContactImageReq req = new UploadContactImageReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.contactId = 31182321L;
		req.type = "2";
		req.certNo = "粤A654321";
		req.certImage = Base64.ToBase64String(getImage("d:/DomainCertificate.jpg"));
		com(req,url);
	}
	
	/**
	 * 更新联系人详细信息
	 * @throws Exception
	 */
	public static void testModifyContactDetail()throws Exception{
		String url = Test.URLPATH + "API/ApiContact/modifyContactDetail.json";
		ModifyContactDetailReq req = new ModifyContactDetailReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		
		req.contactId=1L;
		req.type = "INTERNATIONAL";
		req.owner="张三";
		req.owner_en="zhang san";
		req.contacter_first_name="张";
		req.contacter="三";
		req.contacter_first_name_en="zhang";
		req.contacter_en="san";
		req.email="12324@qq.com";
		req.country="中国";
		req.province="广东";
		req.province_en="GuangDong";
		req.city="广州";
		req.city_en="GuangZhou";
		req.area="天河区";
		req.addr="林和西路1号国际贸易中心31楼";
		req.addr_en="Lin He Xi Lu 1Hao Guo Ji Mao Yi Zhong Xin 31Lou ";
		req.postcode="510000";
		req.tel="+86.18612345678";
		req.fax="+86.2066222604";
		
		com(req,url);
	}
	
	/**
	 * 删除联系人
	 * @throws Exception
	 */
	public static void testDeleteContact()throws Exception{
		String url = Test.URLPATH + "API/ApiContact/deleteContact.json";
		DeleteContactReq req = new DeleteContactReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.contactId = 1L;
		com(req,url);
	}
	
	/**
	 * 添加域名解析
	 * @throws Exception
	 */
	public static void testCreateDns()throws Exception{
		String url = Test.URLPATH + "API/ApiDns/createDns.json";
		CreateDnsReq req = new CreateDnsReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.domain = "dailishangpingtai.com";
		req.rr="*";
		req.type="CNAME";
		req.content="dailishangpingtai.com";
		req.ttl="3600";
		req.mx="";
		com(req,url);
	}
	
	/**
	 * 修改域名解析
	 * @throws Exception
	 */
	public static void testModifyDns()throws Exception{
		String url = Test.URLPATH + "API/ApiDns/modifyDns.json";
		ModifyDnsReq req = new ModifyDnsReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.domain = "adadsf.com";
		req.dnsRecordId=11L;
		req.rr="www";
		req.type="CNAME";
		req.content="yuminghome.com";
		req.ttl="3600";
		req.mx="";
		com(req,url);
	}
	
	/**
	 * 启用、暂停、删除域名解析记录
	 * @throws Exception
	 */
	public static void testModifyDnsStatus()throws Exception{
		String url = Test.URLPATH + "API/ApiDns/modifyDnsStatus.json";
		ModifyDnsStatusReq req = new ModifyDnsStatusReq();
		req.userid = Test.USERID;
		req.apikey= Test.APIKEY;
		req.dnsRecordId = 11L;
		req.flag = "S";
		com(req,url);
	}
	
	
	private static byte [] getImage(String filePath)throws Exception{
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(filePath));
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);    
        byte[] temp = new byte[1024];        
        int size = 0;        
        while ((size = in.read(temp)) != -1) {        
            out.write(temp, 0, size);        
        }        
        in.close();  
        return out.toByteArray();  
	}

}
